package tw.twandroidproj.app.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



import com.koushikdutta.async.future.FutureCallback;
import com.orm.SugarRecord;

import java.util.List;

import tw.twandroidproj.app.model.Auth;

import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.model.User;
import tw.twandroidproj.app.R;

import tw.twandroidproj.app.network.AuthManager;
import tw.twandroidproj.app.network.OnResponseListener;

/**
 * Created by freeze on 2015/4/25.
 *
 * 2015/9  Done
 *
 *
 */
public class WelcomeActivity extends Activity
        implements FutureCallback<User>{
    private Intent intent,i2;
    private AuthManager authManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        Handler x = new Handler();
        x.postDelayed(new splashhandler(), 500);


    }
    class splashhandler implements Runnable{

        public void run() {
            intent=new Intent(WelcomeActivity.this,MainActivity.class);
            i2=new Intent(WelcomeActivity.this,LoginActivity.class);
            final List<Auth> t_list=SugarRecord.listAll(Auth.class);
            authManager=AuthManager.getInstance(WelcomeActivity.this);
             if(t_list.size()==0){

                startActivity(intent);
                WelcomeActivity.this.finish();
                return;
            }



            authManager.refresh(t_list.get(0).getToken(), new OnResponseListener<String>() {
                @Override
                public void onSuccess(String result) {
                    startActivity(intent);
                    finish();
                    Toast.makeText(getBaseContext(),"欢迎登录",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailed(ErrorModel errorModel) {
                    if(errorModel.getStatus_code()==-1){
                        authManager.logout();
                        startActivity(intent);
                        finish();
                        Toast.makeText(getBaseContext(), "请检查网络连接", Toast.LENGTH_SHORT).show();
                        }
                    else if(errorModel.getStatus_code()==0) {
                        authManager.logout();
                        startActivity(i2);
                        finish();
                        Toast.makeText(getBaseContext(), "登录失效，请重新登录", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            startActivity(intent);
            WelcomeActivity.this.finish();

        }

    }
    /**
     //     * 登录请求的回调函数
     //     * @param e
     //     * @param user
     //     */
    @Override
    public void onCompleted(Exception e, User user) {
        if(user != null && user.getNickname() != null){
            //登录成功
        }else {
            //登录失败
            Toast.makeText(this, "登录失败,请重新登录 "+e,Toast.LENGTH_SHORT).show();
        }
        startActivity(intent);
        this.finish();
    }
//    public static boolean isNetworkAvailable(Context context) {
//        ConnectivityManager cm = (ConnectivityManager) context
//                .getSystemService(Context.CONNECTIVITY_SERVICE);
//      if(cm.getActiveNetworkInfo()!=null) {
//            return cm.getActiveNetworkInfo().isAvailable();
//                }
//
//
//        return false;
//    }
}
